<template>
	<view class="member">
		<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption"
			:up="upOption">
			<block v-for="(item,index) in list" :key="index">
				<view class="member_item" @click="selectMember(item)" v-if="item.is_old == 0">
					<view class="member_info" v-if="item.balanceNums == 0 || item.timeFlag == true">
						<radio :disabled="true" :checked="isChecked(item.id)"
							style="transform: scale(.8);flex-shrink: 0;margin-right: 8rpx;" color="#F12F63" />

						<view class="member_text">
							<view style="margin-bottom: 12rpx;">本月可使用 <text
									style="color: #F12F63; font-size: 36rpx;">{{item.balanceNums}} </text>升</view>
							<!-- <view>每月限 <text style="color: #F12F63; font-size: 36rpx;">{{item.limit}}</text>  升</view> -->
							<view class="">
								<text>本轮周期 {{item.epicycle}}</text>
							</view>
							<text>有效期至 {{item.expiretime}}</text>
						</view>
					</view>
					<view class="member_info" v-else>
						<radio :checked="isChecked(item.id)"
							style="transform: scale(.8);flex-shrink: 0;margin-right: 8rpx;" color="#F12F63" />

						<view class="member_text">
							<view style="margin-bottom: 12rpx;">本月可使用 <text
									style="color: #F12F63; font-size: 36rpx;">{{item.balanceNums}} </text>升</view>
							<!-- <view>每月限 <text style="color: #F12F63; font-size: 36rpx;">{{item.limit}}</text>  升</view> -->
							<view class="">
								<text>本轮周期 {{item.epicycle}}</text>
							</view>
							<text>有效期至 {{item.expiretime}}</text>
						</view>
					</view>

					<view class="member_money">
						<text>剩余</text>
						<view class="oil-item-data">
							<text>￥</text>
							{{(item.oil_balance)}}
							<text style="color: #F12F63;">元</text>
						</view>
					</view>
				</view>
				<view class="member_item" @click="selectMember(item)" v-else>
					<view class="member_info" v-if="item.limit == item.monthNums">
						<radio :disabled="true" :checked="isChecked(item.id)"
							style="transform: scale(.8);flex-shrink: 0;margin-right: 8rpx;" color="#F12F63" />

						<view class="member_text">
							<!-- <view style="margin-bottom: 12rpx;">本月可使用 <text style="color: #F12F63; font-size: 36rpx;">{{item.balanceNums}} </text>升</view> -->
							<!-- <view>每月限 <text style="color: #F12F63; font-size: 36rpx;">{{item.limit}}</text>  升</view> -->
							<text>有效时间 不限</text>
						</view>
					</view>
					<view class="member_info" v-else>
						<radio :checked="isChecked(item.id)"
							style="transform: scale(.8);flex-shrink: 0;margin-right: 8rpx;" color="#F12F63" />

						<view class="member_text">
							<!-- <view style="margin-bottom: 12rpx;">本月可使用 <text style="color: #F12F63; font-size: 36rpx;">{{item.balanceNums}} </text>升</view> -->
							<!-- <view>每月限 <text style="color: #F12F63; font-size: 36rpx;">{{item.limit}}</text>  升</view> -->
							<text>有效时间 不限</text>
						</view>
					</view>

					<view class="member_money">
						<text>剩余</text>
						<view class="oil-item-data">
							<text>￥</text>
							{{(item.oil_balance)}}
							<text style="color: #F12F63;">元</text>
						</view>
					</view>
				</view>
			</block>
			<mescroll-empty v-if="list.length==0"></mescroll-empty>
		</mescroll-body>

		<view class="member-confirm">
			<view class="member-btn" @click="checkedMember()">
				<text>确定</text>
			</view>
		</view>
	</view>
</template>

<script>
	import api from '@/api/mine.js';
	import currentDate from '@/commom/js/currentDate.js';
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				list: [],
				id: '',
				member: {},
				now: '', //当前时间
				timeFlag:true,
				showRadio: false, // 默认radio不显示，当加油需要选择优惠券时显示radio
				downOption: {
					use: true,
				},
				upOption: {
					use: true,
					auto: true,
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 15, // 每页数据的数量
						time: null // 加载第一页数据服务器返回的时间; 防止用户翻页时,后台新增了数据从而导致下一页数据重复;
					},
					empty: {
						use: false, // 是否显示空布局
						icon: "https://www.mescroll.com/img/mescroll-empty.png", // 图标路径
						tip: '~ 暂无相关数据 ~', // 提示
						btnText: '去逛逛 >', // 按钮
						fixed: false, // 是否使用fixed定位,默认false; 配置fixed为true,以下的top和zIndex才生效 (transform会使fixed失效,最终会降级为absolute)
						top: "100rpx", // fixed定位的top值 (完整的单位值,如 "10%"; "100rpx")
						zIndex: 99 // fixed定位z-index值
					}
				},
			};
		},

		onLoad(options) {
			this.id = options.station_id;
			this.now = currentDate.getDate();
		},

		computed: {
			isChecked() {
				return function(id) {
					// if(this.timeFlag == false){
						return this.member.id * 1 === id * 1
					// }
				};
			}
		},

		methods: {
			downCallback() {
				this.mescroll.resetUpScroll();
			},

			upCallback(page) {
				let data = {
					page: page.num,
					token: uni.getStorageSync('userinfo').token,
					station_id: this.id
				};
				let _this = this;
				api.getMemberMoney(data).then(res => {
					this.mescroll.endBySize(res.data.data.length, res.data.total);
					//设置列表数据
					if (page.num == 1) this.list = [];
					_this.list = _this.list.concat(res.data.data);
					_this.list.forEach((item, index) => { //js遍历数组
						let obj1 = new Date(item.oil_money_time) //购买时间
						let obj = new Date(_this.now) //有效期开始时间
						if (obj.getTime() > obj1.getTime()) {
							item.timeFlag = false
							console.log("已到可用时间")
						} else {
							item.timeFlag = true
							console.log("未到");
						}
					});
					if (_this.list.length > 0) {
						console.log(_this.now);
						_this.member = _this.list[0];
					} else {
						_this.member = null;
					};
				}).catch(() => {
					this.mescroll.endErr();
				});
			},

			checkedMember() {
				let _this = this
				uni.navigateBack({
					delta: 1,
					success() {
						uni.$emit('memberMoney_info', _this.member);
					}
				});
			},

			selectMember(item) {
				let arr = {}
				arr = item
				this.member = arr
				item.showRadio = !item.showRadio;
				if (item.balanceNums == 0 || item.timeFlag == true) {
					console.log(111);
					item.showRadio = false
				}
				if (item.showRadio == false) {
					this.member = {}
				} else {
					// this.isChecked()
				}
			}
		}
	}
</script>

<style lang="scss">
	.member {
		background-color: #F3F3F3;

		.member_item {
			min-height: 200rpx;
			display: flex;
			align-items: center;
			border-radius: 20rpx;
			background-color: #FFF;
			overflow: hidden;
			margin: 30rpx 30rpx 0;
			box-sizing: border-box;

			.member_money {
				min-height: 200rpx;
				width: 30%;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				background-color: #fff;
				border-radius: 0 16rpx 16rpx 0;
				border-left: 2rpx dashed #e5e5e5;
				box-sizing: border-box;

				.oil-item-data {
					color: #F12F63;
					font-size: 40rpx;
					font-weight: bold;

					text {
						font-size: 30rpx;
					}
				}
			}

			.member_info {
				flex: 1;
				height: 100%;
				background-color: #fff;
				display: flex;
				align-items: center;
				padding: 10rpx 30rpx;
				border-radius: 16rpx 0 0 16rpx;
				box-sizing: border-box;

				.member_text {
					display: flex;
					flex-direction: column;
					font-size: 24rpx;

					text {
						margin: 12rpx 0;
					}
				}
			}
		}

		.member-confirm {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			padding: 20rpx;
			display: flex;
			justify-content: flex-end;
			align-items: center;
			background-color: #fff;
			box-sizing: border-box;

			.member-btn {
				width: 180rpx;
				height: 64rpx;
				line-height: 64rpx;
				background-color: #F12F63;
				font-size: 28rpx;
				color: #fff;
				text-align: center;
				border-radius: 8rpx;
			}
		}
	}
</style>
